package com.zhoug.commonlib2.notifycation;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;

import com.zhoug.logger2.Logger;

import androidx.annotation.Nullable;

/**
 * @Author: zhoug
 * @Date: 2024-11-16
 * @Description:
 */
public class MyTaskService extends Service {
    private static final String TAG = ">>>MyTaskService";
    private static boolean isStart = false;

    public static void start(Context context) {
        Intent intent = new Intent(context, MyTaskService.class);
        context.startService(intent);
    }

    public static void stop(Context context) {
        Intent intent = new Intent(context, MyTaskService.class);
        context.stopService(intent);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Logger.d(TAG, "onCreate:创建任务服务");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Logger.d(TAG, "onStartCommand:");
        if (!isStart) {
            isStart = true;
            new Thread(() -> {
                while (isStart) {
                    MyForeService.start(this);
                    try {
                        Thread.sleep(15_000);
                    } catch (InterruptedException e) {
                        Logger.e(TAG, e.getMessage());
                    }
                }

            }).start();
        }

        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        isStart = false;
        Logger.d(TAG, "onDestroy:关闭任务服务");
    }

}
